<template>
	<el-container>

		<el-header>
			<div class="left-panel">
				<!-- <el-button type="primary" icon="el-icon-plus" @click="add"></el-button> -->
                <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
				<el-button type="info" icon="el-icon-refresh" @click="refresh"></el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.keyword" placeholder="昵称/手机号检索" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="list.apiObj" row-key="id"  @selection-change="selectionChange" stripe>
                <el-table-column type="selection" width="50"></el-table-column>
                <el-table-column label="头像" prop="goods" width="95">
                    <template #default="scope">
                        <el-image class="feng" :src="scope.row.avator" :fit="fit" />
                    </template>

                </el-table-column>
				<el-table-column label="用户信息" prop="info" width="350">
                    <template #default="scope">
                        <p>昵称：{{ scope.row.wx_name }}</p>
                        <p>姓名：{{ scope.row.username }}</p>
                        <p>手机：{{ scope.row.phone }}</p>
                        <p>年龄：{{ scope.row.age==0?'未知':scope.row.age }}</p>
                        <p>性别：{{ scope.row.sex==0?'未知':scope.row.sex==1?'男':'女' }}</p>
						<p>生日：{{scope.row.birthday ?? '-'}}</p>
                    </template>
                </el-table-column>
                <el-table-column label="余额/积分" prop="title" width="150">
                    <template #default="scope">
                        <p>余额：{{ scope.row.balance }}</p>
                        <p>积分：{{ scope.row.jifen }}</p>
                    </template>

                </el-table-column>
				<el-table-column label="消费金额" prop="0" width="190" align="center"></el-table-column>
				<el-table-column label="注册时间" prop="create_time" width="190"></el-table-column>
                <el-table-column label="注册ip" prop="ip" width="190"></el-table-column>
                <el-table-column label="状态" prop="status" width="190">
                    <template #default="scope">
                        <el-tag type="success" v-if="scope.row.status==1">正常</el-tag>
                        <el-tag type="danger" v-if="scope.row.status==0">停用</el-tag>
                    </template>

                </el-table-column>
				<el-table-column label="操作" fixed="right" align="center">
					<template #default="scope">
						<el-button-group>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
                            <el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
							<el-button text type="primary" size="small" @click="tan(scope.row, 'sms')">发送短信</el-button>
							<el-button text type="primary" size="small" @click="tan(scope.row,'mess')">发送站内信</el-button>
							<el-button text type="primary" size="small" @click="tan(scope.row,'balance')">调整余额</el-button>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>

		<el-dialog v-model="balance_tan" title="调整余额" width="600px" :before-close="handleClose">
               <div style="padding: 20px;">
				        <el-form :model="form" label-width="auto" style="max-width: 600px;margin-top: 20px;">
							<el-form-item label="昵称">
								<el-input v-model="form.wx_name" type="text" disabled="true"/>
							</el-form-item>
							<el-form-item label="手机">
								<el-input v-model="form.tel" type="number" disabled="true"/>
							</el-form-item>
							<el-form-item label="类型">
								<el-radio-group v-model="form.ps">
									<el-radio value="1" label="1">增加</el-radio>
									<el-radio value="2" label="2">减少</el-radio>
								</el-radio-group>
							</el-form-item>
							<el-form-item label="变动金额">
							   <el-input v-model="form.money" type="number"/>
							</el-form-item>
							<el-button type="primary" @click="onSubmit('balance')" size="large">确定调整</el-button>
						</el-form>
			   </div>
		</el-dialog>

		<el-dialog v-model="mess_tan" title="发送站内信" width="600px" :before-close="handleClose">
               <div style="padding: 20px;">
				        <el-form :model="form" label-width="auto" style="max-width: 600px;margin-top: 20px;">
							<el-form-item label="昵称">
								<el-input v-model="form.wx_name" type="text" disabled="true"/>
							</el-form-item>
							<el-form-item label="手机">
								<el-input v-model="form.tel" type="number" disabled="true"/>
							</el-form-item>
							<el-form-item label="标题">
								<el-input v-model="form.title" type="text" placeholder="如：充值金额到账通知"/>
							</el-form-item>
							<el-form-item label="内容">
								<el-input v-model="form.mess" type="textarea" />
							</el-form-item>

							<el-button type="primary" @click="onSubmit('mess')" size="large">确定发送</el-button>
						</el-form>
			   </div>
		</el-dialog>
		<el-dialog v-model="sms_tan" title="发送短信" width="600px" :before-close="handleClose">
               <div style="padding: 20px;">
				        <el-form :model="form" label-width="auto" style="max-width: 600px;margin-top: 20px;">
							<el-form-item label="昵称">
								<el-input v-model="form.wx_name" type="text" disabled="true"/>
							</el-form-item>
							<el-form-item label="手机">
								<el-input v-model="form.tel" type="number" disabled="true"/>
							</el-form-item>
							<el-form-item label="短信内容">
								<el-input v-model="form.mess" type="textarea" />
							</el-form-item>

							<el-button type="primary" @click="onSubmit('sms')" size="large">确定发送</el-button>
						</el-form>
			   </div>
		</el-dialog>



	</el-container>
    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
	import saveDialog from './save'
    import scSelectFilter from '@/components/scSelectFilter'
	export default {
		name: 'goods',
		components: {
			saveDialog,
            scSelectFilter
		},
		data() {
			return {
				balance_tan:false,
				mess_tan:false,
				sms_tan:false,
                selection: [],
				form:{
					ps:0,
					money:'',
					wx_name:'',
					tel:'',
					uid:0
				},
				filterData: [
					{
						title: "商品分类",
						key: "cid",
						multiple: false,
						options: []
					}
				],
                dialog: {
					save: false,
					permission: false
				},
				list: {
					apiObj: this.$API.system.customer.list
				},
				search: {
					keyword: ""
				}
			}
		},
        mounted() {

		},
		methods: {
			async onSubmit(type=''){
				this.form.act = type
				var res = await this.$API.system.universal.handle.post(this.form,'/user/edit');
				if(res.code == 200){
					this.$refs.table.refresh()
					this.balance_tan = false
					this.mess_tan = false
					this.sms_tan = false
					if(type=='mess'){
						 this.form.mess=''
						 this.form.title=''
					}
					this.$message.success("操作成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
            tan(arr,type=''){

				this.form.wx_name = arr.wx_name
				this.form.tel = arr.phone
				this.form.uid = arr.uid
				 if(type=='balance'){
					this.balance_tan = true
				 }
				 if(type=='mess'){
					this.mess_tan = true
				 }
				 if(type=='sms'){
					this.sms_tan = true
				 }

			},
            //添加
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
            //删除
			async table_del(row){
				var reqData = {uid: row.uid}
				//var res = await this.$API.demo.user_del.post(reqData);
				var res = await this.$API.system.universal.handle.post(reqData,'/user/user_del');
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
            //表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},
            //批量删除
			async batch_del(){
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？如果删除项中含有子集将会被一并删除`, '提示', {
					type: 'warning'
				}).then(() => {
					var arr = this.selection
					var ob = []
					for(var i=0;i<arr.length;i++){
						  ob.push(arr[i].gid)
					}
					this.table_del({'gid':ob.join(",")})
				}).catch(() => {

				})
			},
			//搜索
			upsearch(){
				this.$refs.table.upData(this.search)
			},
			//标签切换
			tabChange(name){
                this.status = name
                this.$refs.table.reload({'cid':this.cid,'status':this.status})
			},
			filterChange(data){
                this.cid = data.cid
                this.$refs.table.reload({'cid':this.cid,'status':this.status})
				//this.$refs.table.upData(data)
			},
            refresh(){
                this.$refs.table.reload()
            },
            	//本地更新数据
			handleSaveSuccess(data, mode){
				if(mode=='add' || mode=='edit'){
					this.$refs.table.reload()
				}
			}
		}

	}
</script>

<style>
.pro{
    width:100%;
    float: left;
}
.feng{
    widows:40px;
    height:40px;
    border-radius: 995px;
}
.protitle{
    width: 80%;
    float: left;
    padding-left: 10px;
}
</style>
